package net.xo68.destiny.starter.common;

import lombok.extern.slf4j.Slf4j;
import net.xo68.destiny.contract.common.SystemConstants;
import net.xo68.destiny.core.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import java.util.Arrays;

/**
 * 启动运行信息输出
 * @author wuxie
 * @version 2019-9-2
 */
@Slf4j
@Component
public class StartupRuner implements ApplicationRunner {


    @Autowired
    private Environment environment;
    @Autowired
    private SchedulerCommonProperties schedulerCommonProperties;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        StringBuilder logFormatSb=new StringBuilder();
        logFormatSb.append("\n---------------------------------\n");
        logFormatSb.append("      应用名称: {}\n");
        logFormatSb.append("          端口: {}\n");
        logFormatSb.append("ActiveProfiles: {}\n");
        logFormatSb.append("    Local: http://localhost:{}\n");
        logFormatSb.append(" External: http://{}:{}\n");
        logFormatSb.append("      doc: http://{}:{}/doc.html\n");
        logFormatSb.append("------------------------------------");
        log.info(logFormatSb.toString(),
                environment.getProperty("spring.application.name"),
                environment.getProperty(SystemConstants.CONFIG_SERVER_PORT),
                environment.getActiveProfiles(),
                environment.getProperty(SystemConstants.CONFIG_SERVER_PORT),
                IpUtil.getIp(schedulerCommonProperties.getNodeRealIp()),
                environment.getProperty(SystemConstants.CONFIG_SERVER_PORT),
                IpUtil.getIp(schedulerCommonProperties.getNodeRealIp()),
                environment.getProperty(SystemConstants.CONFIG_SERVER_PORT));

        boolean isDev= Arrays.stream(environment.getActiveProfiles()).anyMatch((profile)->{
            return "dev".equals(profile);
        });

        log.info("是否dev环境：{}",isDev);
    }
}
